package com.jflm.controller.api;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.jflm.base.BaseAction;
import com.jflm.domain.RuleData;
import com.jflm.service.IRuleDataService;

@RestController
@RequestMapping("/api/ruleData")
public class RuleDataAction extends BaseAction{
	private static final Logger LOG = LoggerFactory.getLogger(RuleDataAction.class);
	
	@Autowired
	private IRuleDataService ruleDataService;
	
	/**
	 * ******************************************** method name : findList
	 * description : 查询全部规则
	 * 
	 * @return : List<RuleData>
	 * @param : @return
	 * @param : @throws Exception
	 * @see : *******************************************
	 */
	@PostMapping(value = "/findList")
	public List<RuleData> findList(@RequestBody RuleData ruleData)
			throws Exception {
		//LOG.info("#findList() 请求参数：{}", gson.toJson(ruleData));
		List<RuleData> reList = ruleDataService.findList(ruleData);
		//LOG.info("#findList() 响应参数：{}", gson.toJson(reList));
		return reList;
	}
	/**
	 * ******************************************** method name : findList
	 * description : 查询单条
	 * 
	 * @return : List<RuleData>
	 * @param : @return
	 * @param : @throws Exception
	 * @see : *******************************************
	 */
	@RequestMapping(value = "/findOne", method = RequestMethod.POST)
	public RuleData findOne(@RequestBody RuleData ruleData)
			throws Exception {
		RuleData ruleData2= ruleDataService.findOne(ruleData.getId());
		LOG.info("#findOne() 响应参数：{}", gson.toJson(ruleData));
		return ruleData2;
	}
	
	/**
	 * ******************************************** method name : add
	 * description : 添加规则
	 * 
	 * @return : 
	 * @param : @return
	 * @param : @throws Exception
	 * @see : *******************************************
	 */
	@PostMapping(value="/addPage")
	public int add(@RequestBody RuleData ruleData) throws Exception{
//		System.out.println(gson.toJson(ruleData)+"============================");
		UUID uuid = UUID.randomUUID();
		
		
		Date date = new Date();       
		Timestamp nowdate = new Timestamp(date.getTime()); 
		ruleData.setCreateTime(nowdate);
		ruleData.setUpdateTime(nowdate);
		ruleData.setLevel(1);
		ruleData.setPriority(1);
		ruleData.setDel(0);
		ruleData.setUpdateUser("admin");
		
		ruleData.setType(ruleData.getDb_source());
		
		ruleData.setId(uuid.toString());
		
		//获取6位随机数  
	    int random=(int) ((Math.random()+1)*100000);   
	    String ruleCode = "FLK-"+random; 
	    ruleData.setRuleCode(ruleCode);
		
		if (ruleData.getMinValue()!=null) {
			
			String section = ruleData.getMinValue()+"<值<"+ruleData.getMxValue();
			ruleData.setSection(section);
		}else {
			String section = ruleData.getRule_section2();
			ruleData.setSection(section);
		}
//		System.out.println(gson.toJson(ruleData)+"=========================");
		int flag = ruleDataService.add(ruleData);
		return flag;
	}
	
	
	/**
	 * ******************************************** method name : 
	 * description : 添加规则类型
	 * @return : 
	 * @param : @return
	 * @param : @throws Exception
	 * @see : *******************************************
	 *//*
	@PostMapping(value="/addaPage")
	public int adda(RuleData ruleData) throws Exception{
		UUID uuid = UUID.randomUUID();
		ruleData.setId(uuid.toString());
		ruleData.setType(ruleData.getType());
		int flag = ruleDataService.add(ruleData);
		return flag;
	}*/
	
	/**
	 * ******************************************** method name : update
	 * description : 修改规则
	 * 
	 * @return : 
	 * @param : @return
	 * @param : @throws Exception
	 * @see : *******************************************
	 */
	@PostMapping(value = "/updatePage")
	public int updatePage(@RequestBody RuleData ruleData) throws Exception {
		if (ruleData.getMinValue()!=null) {
			String section = ruleData.getMinValue()+"<值<"+ruleData.getMxValue();
			ruleData.setSection(section);
		}else {
			String section = ruleData.getRule_section2();
			ruleData.setSection(section);
		}
		ruleData.setType(ruleData.getDb_source());
		int rul = ruleDataService.update(ruleData);
		return rul;
	}
	
	/**
	 * ******************************************** method name : 
	 * description : 删除单条
	 * 
	 * @return : List<RuleData>
	 * @param : @return
	 * @param : @throws Exception
	 * @see : *******************************************
	 */
	
	@RequestMapping(value = "/deletePage", method = RequestMethod.POST)
	public int deletePage(@RequestBody RuleData ruleData) throws Exception {
		int rul = ruleDataService.delete1(ruleData);
		//LOG.info("#findOne() 响应参数：{}", gson.toJson(ruleData));
		return rul;
	}
	
	
}
